package gcosc.fun.hly.fastsso.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gcosc.fun.hly.fastsso.domain.Bo.PageQuery;
import gcosc.fun.hly.fastsso.domain.SsoApplication;
import gcosc.fun.hly.fastsso.domain.SsoRole;
import gcosc.fun.hly.fastsso.domain.SsoUser;
import gcosc.fun.hly.fastsso.mapper.SsoApplicationMapper;
import gcosc.fun.hly.fastsso.result.Code;
import gcosc.fun.hly.fastsso.result.TableDataInfo;
import gcosc.fun.hly.fastsso.service.ISsoApplicationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 应用程序Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-30
 */
//@RequiredArgsConstructor
@Service
public class SsoApplicationServiceImpl extends ServiceImpl<SsoApplicationMapper, SsoApplication> implements ISsoApplicationService {

    /**
     * 查询应用程序
     */
    @Override
    public SsoApplication queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询应用程序列表
     */
    @Override
    public TableDataInfo<SsoApplication> queryPageList(SsoApplication bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SsoApplication> lqw = buildQueryWrapper(bo);
        IPage<SsoApplication> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询应用程序列表
     */
    @Override
    public List<SsoApplication> queryList(SsoApplication bo) {
        LambdaQueryWrapper<SsoApplication> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SsoApplication> buildQueryWrapper(SsoApplication bo) {
        LambdaQueryWrapper<SsoApplication> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getApplication()), SsoApplication::getApplication, bo.getApplication());
        lqw.eq(bo.getStatus() != null, SsoApplication::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceAddress()), SsoApplication::getSourceAddress, bo.getSourceAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getRealm()), SsoApplication::getRealm, bo.getRealm());
        return lqw;
    }

    /**
     * 新增应用程序
     */
    @Override
    public Boolean insertByBo(SsoApplication bo) {
        SsoApplication add = BeanUtil.toBean(bo, SsoApplication.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改应用程序
     */
    @Override
    public Boolean updateByBo(SsoApplication bo) {
        SsoApplication update = BeanUtil.toBean(bo, SsoApplication.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SsoApplication entity){
        //TODO 做一些数据校验,如唯一约束
        SsoApplication one = lambdaQuery().eq(SsoApplication::getRealm, entity.getRealm())
                .eq(SsoApplication::getApplication, entity.getApplication()).one();
        if (one != null && entity.getId() == null ||
                entity.getId() != null && !one.getId().equals(entity.getId())){
            Code.BUSINESS_ERR("应用已经存在");
        }
    }

    /**
     * 批量删除应用程序
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
